package com.ruoyi.system.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.config.SensorConfig;
import com.ruoyi.system.entity.LightningData;
import com.ruoyi.system.entity.LightningSensor;
import com.ruoyi.system.mapper.LightningDataMapper;
import com.ruoyi.system.mapper.LightningSensorMapper;
import com.ruoyi.system.service.LightningSensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 雷电预警站点(LightningSensor)表服务实现类
 *
 * @author makejava
 * @since 2023-03-08 13:47:13
 */
@Service("lightningSensorService")
public class LightningSensorServiceImpl implements LightningSensorService {
    @Resource
    private LightningSensorMapper lightningSensorMapper;
    @Resource
    private LightningDataMapper lightningDataMapper;
    @Autowired
    private SensorConfig sensorConfig;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LightningSensor queryById(String id) {
        return this.lightningSensorMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param lightningSensor 筛选条件
     * @return 查询结果
     */
    @Override
    public List<LightningSensor> queryByPage(LightningSensor lightningSensor) {
        List<LightningSensor> lightningSensors = this.lightningSensorMapper.queryAllByLimit(lightningSensor);
        return lightningSensors;
    }

    /**
     * 新增数据
     *
     * @param lightningSensor 实例对象
     * @return 实例对象
     */
    @Override
    public LightningSensor insert(LightningSensor lightningSensor) {
        this.lightningSensorMapper.insert(lightningSensor);
        return lightningSensor;
    }

    /**
     * 修改数据
     *
     * @param lightningSensor 实例对象
     * @return 实例对象
     */
    @Override
    public LightningSensor update(LightningSensor lightningSensor) {
        this.lightningSensorMapper.update(lightningSensor);
        return lightningSensor;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.lightningSensorMapper.deleteById(id) > 0;
    }


    // 同步雷电站点
    @Scheduled(cron = "0/60 * * * * ?")
    public void synchSensor() {
        System.out.println("开始同步雷电站点数据");
//        HttpResponse execute = HttpRequest.post("http://" + sensorConfig.getIp() + ":" + sensorConfig.getPort() + sensorConfig.getSynchSensor())
        HttpResponse execute = HttpRequest.post(sensorConfig.getIp() + sensorConfig.getSynchSensor())
                .form("username", sensorConfig.getUserName(), "password", SensorConfig.getPASSWORD()).timeout(3000).execute();
        System.out.println("同步雷电站点数据返回数据" + execute.body());
        int status = execute.getStatus();
        if (status != 200) {
            return;
        }
        String body = execute.body();
        JSONObject jsonObject1 = new JSONObject(body);
        JSONArray jsonArray = jsonObject1.getJSONArray("obj");
        Object o1 = jsonArray.get(0);
        JSONObject jsonObject = new JSONObject(o1);
        String sensorId = jsonObject.getStr("sensorId");
        LightningSensor byId = queryById(sensorId);
        if (null == byId) {
            LightningSensor lightningSensor = new LightningSensor();
            lightningSensor.setSensorId(jsonObject.getStr("sensorId"));
            lightningSensor.setSensorCode(jsonObject.getStr("sensorCode"));
            lightningSensor.setSensorName(jsonObject.getStr("sensorName"));
            lightningSensor.setProvinceName(jsonObject.getStr("areaId"));
            lightningSensor.setCityName(jsonObject.getStr("domainName"));
            lightningSensor.setAreaName(jsonObject.getStr("localName"));
            lightningSensor.setLatitude(jsonObject.getStr("XCoordiNate"));
            lightningSensor.setLongitude(jsonObject.getStr("YCoordiNate"));
            insert(lightningSensor);
        }
        String cookie = execute.header("set-cookie");
        System.out.println("同步雷电站点数据返回cookie值： "  + cookie);
        System.out.println("开始同步雷电站点实时数据");
        HttpResponse executeData = HttpRequest.post(sensorConfig.getIp() + sensorConfig.getSynchData())
                .form("username", sensorConfig.getUserName(), "password", SensorConfig.getPASSWORD()).header("cookie",cookie).timeout(3000).execute();
        System.out.println("同步雷电站点实时数据返回数据" + executeData.body());
        int statusData = executeData.getStatus();
        if (statusData != 200) {
            return;
        }
        String bodyData = executeData.body();
        JSONObject jsonObject2 = new JSONObject(bodyData);
        JSONArray jsonArray2 = jsonObject2.getJSONArray("obj");
        Object o2 = jsonArray2.get(0);
        JSONObject jsonObject3 = new JSONObject(o2);
        LightningData lightningData = JSONUtil.toBean((JSONObject) jsonObject3, LightningData.class);
        lightningData.setIsUpload(0);
        lightningDataMapper.insert(lightningData);
    }
}
